package com.jmxcfc.blfsc.cfca.util;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;

import java.security.MessageDigest;

/**
 * 功能描述
 *
 * @author: pbs
 * @date: 2024年08月28日 16:45
 */
@Slf4j
public class SecurityUtils {
    private SecurityUtils(){
        throw new IllegalStateException("Utility class");
    }


    /**
     * todo 待确认是否可以
     * @param source
     * @return
     * @throws Exception
     */
    public static String digestAndHex(byte[] source) throws Exception {
        log.info("digestAndHex start");
        if (source != null && source.length != 0) {
            byte[] digestBytes = generateHash(source);
            String digest = bytes2hex(digestBytes);
            log.info("digestAndHex end");
            return digest;
        } else {
            return "";
        }
    }

    public static String bytes2hex(byte[] bytes) {
        String result = Hex.toHexString(bytes);
        return result.toUpperCase();
    }

    private static byte[] generateHash(byte[] data) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.reset();
            md.update(data);
            return md.digest();
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

}